# 要添加一个新单元，输入 '# %%'
# 要添加一个新的标记单元，输入 '# %% [markdown]'
# 我发现写到最后，还是学弟写的那种样子。好麻烦啊，感觉应该一天就能写完，但自己在硬拖。
import copy
import torch
from torch.utils import data
from trainers.fedbase.FedClientBase import FedClientBase

import logging
# logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logging.basicConfig(format ='%(message)s')
logger = logging.getLogger()
logger.setLevel(logging.INFO)



class FedMamlClient(FedClientBase):
    """训练相关的参数本来应该是服务器发送过来的，包括模型、参数啥的。训练数据应该是本地的。\n
    这里为了方便，只模拟联邦学习的效果和逻辑，不模拟联邦学习的真是过程。
    1. 数据由服务器分成独立同分布，或者非独立同分布。然后分配给各个客户端。而非客户端在本地读取。
    2. 训练的模型、参数在客户端配置。
    3. 客户端完成训练过程、服务端完成聚合过程。客户端和服务端只发送和传输模型和优化器的权重参数。完成聚合和分配的通信逻辑。
    
    接收参数，完成训练。主要是一次训练的参数。
    每一个算法都应该提供梯度的返回值。即在客户端计算出梯度。其实在服务器计算也无可厚非。
    Returns:
        [type]: [description]
    """    

    def __init__(self,datapair,model):
        super(FedMamlClient, self).__init__(datapair,model)    
        logger.info("FedMamlClient init----")

    def set_parameters(self,client_params):
        """从服务器加载训练的参数。并根据batch_size等参数划分数据集。

        Args:
            client_params (dict): 客户端进行训练的相关参数
        """     
        # 训练基本参数(包含记载数据)---------------------------------------------------------------------------------------
        super().set_parameters(client_params)   
        # FedAmpClient策略参数-------------------------------------------------------------------------
        # fedper数据集划分参数
        if("support_ratio" in client_params):
            self.support_ratio=client_params["support_ratio"]
        else:
            self.support_ratio = 0.7
        logger.info("FedMamlClient set_parameters:support_ratio={}\t ".format(self.support_ratio))

    
    def process_data(self):
        # 在基础的服务器上。默认初始化调用这个函数。这是统一的接口
        self.process_data_support_and_query()

    def process_data_support_and_query(self):
        """注意，这里将support&&query set都设置为一个batch，相当于one batch梯度下降。
        元学习的规定就是这样的，这里改了显然不太合适。

        Args:
            datapair ((x,y)): 数据集
        """        
        x,y = self.datapair
        assert len(x) == len(y)
        sq_point  = int(len(y)*self.support_ratio)
        # 将数据转换成tensor,为了加快速度，通过共享内存的方式
        x_support_tensor = torch.FloatTensor(x[:sq_point]).to(self.device)
        y_support_tensor = torch.from_numpy(y[:sq_point]).to(self.device)
        x_query_tensor = torch.FloatTensor(x[sq_point:]).to(self.device)
        y_query_tensor = torch.from_numpy(y[sq_point:]).to(self.device)
        # print(x_train_tensor.dtype,y_train_tensor.dtype)
        support_ds = data.TensorDataset(x_support_tensor,y_support_tensor)
        self.support_dl = data.DataLoader(support_ds,batch_size=len(support_ds),shuffle=True)
        query_ds  = data.TensorDataset(x_query_tensor,y_query_tensor)
        self.query_dl = data.DataLoader(query_ds,batch_size=len(query_ds),shuffle=True)

        self.iteration  = self.epochs*(len(self.support_dl))
        logger.info("FedMamlClient process_data_support_and_query: iteration_support_dl:{}\t".format(self.iteration))
        return self.support_dl,self.query_dl


    # 用来训练当前的本地模型。每次训练返回损失值running_loss和running_accuracy
    def train_maml(self):
        self.model.train()
        loss_sum = 0.
        loss_times = 0.
        correct_sum = 0.
        total_size = 0        
        # 保留原始梯度的一个副本
        model_state_dict = copy.deepcopy(self.model.state_dict())
        for epoch in range(self.epochs):
            # 使用support_set进行梯度下降
            for batch_x,batch_y in self.support_dl:
                # 正向传播
                outputs = self.model(batch_x)

                # 计算loss
                loss = self.loss_func(outputs,batch_y.long())
                loss_sum +=loss.item()
                loss_times+=1
                # 反向传播
                self.optimizer.zero_grad()
                loss.backward()

                # 梯度下降
                self.optimizer.step()
            
        # 使用query_set进行一次梯度下降
        for batch_x,batch_y in self.query_dl:
            # 正向传播
            outputs = self.model(batch_x)
            _,predicted = torch.max(outputs,1)
            correct_sum += (predicted == batch_y).sum().item()
            total_size += len(batch_y)
            # 计算loss
            loss = self.loss_func(outputs,batch_y.long())
            loss_sum +=loss.item()
            loss_times+=1
            # 反向传播
            self.optimizer.zero_grad()
            loss.backward()

            # 梯度下降，在下降前加载初始的参数状态
            self.model.load_state_dict(model_state_dict)
            self.optimizer.step()
        # print(self.optimizer.state_dict())
        return loss_sum/loss_times,100*correct_sum/total_size

    # grad版本的maml
    def train_maml_by_grad(self):
        self.model.train()
        loss_sum = 0.
        loss_times = 0.
        correct_sum = 0.
        total_size = 0        
        # 保留原始梯度的一个副本
        model_state_dict = copy.deepcopy(self.model.state_dict())
        for epoch in range(self.epochs):
            # 使用support_set进行梯度下降
            for batch_x,batch_y in self.support_dl:
                # 正向传播
                outputs = self.model(batch_x)

                # 计算loss
                loss = self.loss_func(outputs,batch_y.long())
                loss_sum +=loss.item()
                loss_times+=1
                # 反向传播
                self.optimizer.zero_grad()
                loss.backward()

                # 梯度下降
                self.optimizer.step()
            
        # 使用query_set进行一次梯度下降
        for batch_x,batch_y in self.query_dl:
            # 正向传播
            outputs = self.model(batch_x)
            _,predicted = torch.max(outputs,1)
            correct_sum += (predicted == batch_y).sum().item()
            total_size += len(batch_y)
            # 计算loss
            loss = self.loss_func(outputs,batch_y.long())
            loss_sum +=loss.item()
            loss_times+=1
            # 反向传播
            self.optimizer.zero_grad()
            self.grad_dict = torch.autograd.grad(loss,self.model.parameters(),create_graph=True, retain_graph=True)
        # print(self.optimizer.state_dict())
        return loss_sum/loss_times,100*correct_sum/total_size

    # 进行finetuning之后测试
    def test_maml(self):
        self.model.train()
        # 冻结meta层，训练local层。这里可以进行优化，将这样的两个优化器直接放到init中。所有的参数require_grade都是true。但是optimizer只更新部分参数。
        for epoch in range(self.epochs+10):
            # 使用support_set进行梯度下降
            for batch_x,batch_y in self.support_dl:
                # 正向传播
                outputs = self.model(batch_x)

                # 计算loss
                loss = self.loss_func(outputs,batch_y.long())
                # 反向传播
                self.optimizer.zero_grad()
                loss.backward()

                # 梯度下降
                self.optimizer.step()
        # 进入测试
        self.model.eval()
        correct = 0
        total =0
        with torch.no_grad():
            for features,labels in self.query_dl:
                outputs = self.model(features)
                # print(outputs.shape)
                _,predicted = torch.max(outputs,1)
                total += labels.size(0)
                correct += (predicted == labels).sum().item()

        accuracy = 100*correct/total
        return accuracy
